
package com.funo.government.info.utils;

public class BusinessException extends Exception {

    private static final long serialVersionUID = -7340725172187075793L;

    public static final String CODE_ILLEGAL_RETURN = "9999";

    public static final String CODE_UNREACH_SERVER = "9998";

    // 0000响应成功/0001响应失败/0002响应异常/0003连接超时/0004参数错误
    public static final String CODE_RESPONSE_FAIL = "0001";

    public static final String CODE_RESPONSE_EXCEPTION = "0002";

    public static final String CODE_RESPONSE_OUT_TIME = "0003";

    public static final String CODE_PARM_WRONG = "0004";

    private String mCode;

    private String mMessage;

    private String mOriginalResult;

    public BusinessException(String code) {
        mCode = code;
    }

    public BusinessException(String code, String msg) {
        mCode = code;
        mMessage = msg;

    }

    public BusinessException(String code, String msg, String result) {
        mCode = code;
        mMessage = msg;
        mOriginalResult = result;
    }

    public String getCode() {
        return mCode;
    }

    public String getOriginalResult() {
        return mOriginalResult;
    }

    /**
     * 获取实际错误信息
     * 
     * @return
     */
    public String getRealMessage() {
        if (CODE_RESPONSE_FAIL.equals(mCode)) {
            return "响应失败";
        } else if (CODE_RESPONSE_EXCEPTION.equals(mCode)) {
            return "响应异常";
        } else if (CODE_RESPONSE_EXCEPTION.equals(mCode)) {
            return "响应异常";
        } else if (CODE_PARM_WRONG.equals(mCode)) {
            return "参数错误";
        } else if (CODE_ILLEGAL_RETURN.equals(mCode)) {
            return "非法返回";
        } else if (CODE_UNREACH_SERVER.equals(mCode)) {
            return "无法到达服务器";
        }

        return "未知错误";
    }

    /**
     * 获取错误信息，展示给用户
     */
    @Override
    public String getMessage() {

        return "服务超时，请稍后重试";
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("BusinessException result:").append(mCode).append("  desc:")
                .append(getRealMessage());
        return sb.toString();
    }

    @Override
    public void printStackTrace() {

        System.err.println(toString());
    }

}
